package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;

import io.lkt.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.lkt.modules.sys.entity.BUserGroupOpenidEntity;
import io.lkt.modules.sys.service.BUserGroupOpenidService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;

import javax.swing.text.html.Option;


/**
 * openid与userid对照表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2021-09-11 16:20:03
 */
@RestController
@RequestMapping("/sys/busergroupopenid")
public class BUserGroupOpenidController {
    @Autowired
    private BUserGroupOpenidService bUserGroupOpenidService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:busergroupopenid:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bUserGroupOpenidService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:busergroupopenid:info")
    public R info(@PathVariable("id") Integer id){
        BUserGroupOpenidEntity bUserGroupOpenid = bUserGroupOpenidService.getById(id);

        return R.ok().put("bUserGroupOpenid", bUserGroupOpenid);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:busergroupopenid:save")
    public R save(@RequestBody BUserGroupOpenidEntity bUserGroupOpenid){
        bUserGroupOpenidService.save(bUserGroupOpenid);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:busergroupopenid:update")
    public R update(@RequestBody BUserGroupOpenidEntity bUserGroupOpenid){
        ValidatorUtils.validateEntity(bUserGroupOpenid);
        bUserGroupOpenidService.updateById(bUserGroupOpenid);
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:busergroupopenid:delete")
    public R delete(@RequestBody Integer[] ids){
        bUserGroupOpenidService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @RequestMapping("/getUserOtherFollowGzh/{userId}")
    public R getUserOtherFollowGzh(@PathVariable("userId") Integer userId) {
        Integer userOtherFollowGzhCount = bUserGroupOpenidService.getUserOtherFollowGzh(userId);
        return R.ok().put("userOtherFollowGzhCount", Optional.ofNullable(userOtherFollowGzhCount).orElse(0));
    }
}
